//
//  GradeTableViewHeaderView.m
//  GYWBaseAPP
//
//  Created by 郭艺伟 on 16/5/23.
//  Copyright © 2016年 郭艺伟. All rights reserved.
//

#import "GradeTableViewHeaderView.h"


#define kMainTiteleLabelFontSize 14
#define kSubTiteleLabelFontSize 12
#define kReferenceLabelFontSize 14
#define kStandardLabelFontSize 14

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]


#import <Masonry/Masonry.h>

@interface GradeTableViewHeaderView ()
/**
*  所有子视图的父视图
*/
@property (nonatomic, weak) UIView *BGView;


/**
 *  主标题和父标题的父视图
 */
@property (nonatomic, weak) UIView *titleBGView;

/**
 *  主标题
 */
@property (nonatomic, strong) UILabel *mainTiteleLabel;

/**
 *  副标题
 */
@property (nonatomic, strong) UILabel *subTiteleLabel;

/**
 *  参数数据
 */
@property (nonatomic, strong) UILabel *referenceLabel;

/**
 *  考评标准
 */
@property (nonatomic, strong) UILabel *standardLabel;





@end

@implementation GradeTableViewHeaderView

+(instancetype)headerViewWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"GradeTableViewHeaderView";
    GradeTableViewHeaderView *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (cell == nil) {
        cell = [[GradeTableViewHeaderView alloc] initWithReuseIdentifier:ID];
    }
    return cell;
}


-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        //1.设置cell的背景颜色背景图
        self.backgroundColor = [UIColor clearColor];
        self.backgroundView = nil;
        //2.添加所有子视图
        [self setupAllSupViews];
    }
    return self;
}


-(void)setupAllSupViews
{
    //1.BGView(所有子视图的父视图)
    UIView *BGView = [[UIView alloc] init];
    BGView.backgroundColor = [UIColor orangeColor];
    BGView.layer.masksToBounds = YES;
    [self.contentView addSubview:BGView];
    self.BGView = BGView;
    
    UIView *titleBGView = [[UIView alloc] init];
    titleBGView.backgroundColor = [UIColor redColor];
    titleBGView.layer.masksToBounds = YES;
    [self.BGView addSubview:titleBGView];
    self.titleBGView = titleBGView;
    
    
    //1.主标题label
    UILabel *mainTiteleLabel = [[UILabel alloc] init];
    mainTiteleLabel.backgroundColor = BGView.backgroundColor;
    mainTiteleLabel.backgroundColor = [UIColor blueColor];
    mainTiteleLabel.font = [UIFont systemFontOfSize:kMainTiteleLabelFontSize];
    //gradeLabel.textColor = UIColorFromRGB(0xffb32e);
    [BGView addSubview:mainTiteleLabel];
    _mainTiteleLabel = mainTiteleLabel;
    _mainTiteleLabel.text = @"行业信息及人才库建设";
    
    
    //2.副标题label
    UILabel *subTiteleLabel = [[UILabel alloc] init];
    subTiteleLabel.backgroundColor = BGView.backgroundColor;
    subTiteleLabel.backgroundColor = [UIColor brownColor];
    subTiteleLabel.font = [UIFont systemFontOfSize:kSubTiteleLabelFontSize];
    subTiteleLabel.textAlignment = NSTextAlignmentCenter;
    //subTiteleLabel.textColor = UIColorFromRGB(0xffb32e);
    [BGView addSubview:subTiteleLabel];
    _subTiteleLabel = subTiteleLabel;
    _subTiteleLabel.text = @"储备行业和关键岗位人选信息的数量及质量(储备人才达到测试地点规定的高度)";
    
    
    //2.分数标题label
    UILabel *referenceLabel = [[UILabel alloc] init];
    referenceLabel.backgroundColor = BGView.backgroundColor;
    referenceLabel.backgroundColor = [UIColor brownColor];
    referenceLabel.font = [UIFont systemFontOfSize:kReferenceLabelFontSize];
    //scoresLabel.textColor = UIColorFromRGB(0xffb32e);
    [BGView addSubview:referenceLabel];
    _referenceLabel = referenceLabel;
    
    
    
    //4.描述标题label
    UILabel *standardLabel = [[UILabel alloc] init];
    standardLabel.backgroundColor = BGView.backgroundColor;
    standardLabel.backgroundColor = [UIColor redColor];
    standardLabel.font = [UIFont systemFontOfSize:kStandardLabelFontSize];
    //standardLabel.textColor = UIColorFromRGB(0x666666);
    [BGView addSubview:standardLabel];
    _standardLabel = standardLabel;
    
    
    [self.BGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.mainTiteleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.BGView).offset(8);
        make.left.equalTo(self.BGView).offset(10);
        make.right.equalTo(self.BGView).offset(-10);
        make.height.mas_equalTo(kMainTiteleLabelFontSize+3);
    }];
    
    
    [self.subTiteleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainTiteleLabel.mas_bottom).offset(8);
        make.left.equalTo(self.BGView).offset(10);
        make.right.equalTo(self.BGView).offset(-10);
    }];
    
    [self.titleBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.BGView).offset(0);
        make.left.equalTo(self.BGView).offset(0);
        make.right.equalTo(self.BGView).offset(0);
        make.bottom.equalTo(self.subTiteleLabel.mas_bottom).offset(8);
    }];
    
    
    [self.referenceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleBGView.mas_bottom).offset(14);
        make.left.equalTo(self.BGView).offset(22);
        make.right.equalTo(self.BGView).offset(-22);
        make.height.mas_equalTo(kReferenceLabelFontSize+3);
        
    }];
    
    [self.standardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.referenceLabel.mas_bottom).offset(10);
        make.left.equalTo(self.BGView).offset(22);
        make.right.equalTo(self.BGView).offset(-22);
    }];
    
}

- (void)setHeader:(GradeHeader *)header
{
    _header = header;
    
    if (header.mainTitle) {
        _mainTiteleLabel.text = header.mainTitle;
        _mainTiteleLabel.hidden = NO;
    }else{
        _mainTiteleLabel.text = nil;
        _mainTiteleLabel.hidden = YES;
    }
    
    if (header.subTitle ) {
        _subTiteleLabel.text = header.subTitle;
        _subTiteleLabel.hidden = NO;
    }else{
        _subTiteleLabel.text = nil;
        _subTiteleLabel.hidden = YES;
    }
    
    if (_subTiteleLabel.hidden) {
        [_titleBGView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_mainTiteleLabel.mas_bottom).offset(8);
        }];
    }else{
        [_titleBGView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_subTiteleLabel.mas_bottom).offset(8);
        }];
    }
    
    if (header.reference) {
        _referenceLabel.text = header.reference;
        _referenceLabel.hidden = NO;
        
    }else{
        _referenceLabel.text = nil;
        _referenceLabel.hidden = YES;
    }
    
    if (header.standard) {
        _standardLabel.text = header.standard;
        _standardLabel.hidden = NO;
    }else{
        _standardLabel.text = nil;
        _standardLabel.hidden = YES;
    }

}

@end
